/**
 * PersonDao.java
 *
 * @author: ZhuJiahui
 * @date: 2018/9/2 23:03
 */
package com.zhujiahui.mybatis.dao;

import com.zhujiahui.mybatis.domain.Person;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @description:
 * @author: ZhuJiahui
 * @version: 1.0
 */
public class PersonDao {

    SqlSession session;

    public PersonDao() {
        //加载 mybatis 配置文件
        InputStream inputStream = PersonDao.class.
                getClassLoader().getResourceAsStream("mybatis-configuration.xml");
        //构建sqlSession的工厂
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);
        //根据 sqlSessionFactory 产生 session
        this.session = sqlSessionFactory.openSession();
    }

    public Person selectById(int id)
    {
        String statement = "personMapper.selectPersonById";
        Person person = this.session.selectOne(statement, id);
        session.close();
        return person;
    }
}
